package com.xing.designmode;

import android.graphics.Color;
import android.text.TextUtils;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.xing.controller.DBController;
import com.xing.model.HsdModelAnswer;

/**
 * 单选题(单纯的一个单选题)
 * @author xing
 *
 */
public class SingleQuestionAnswer extends QuestionAnswer{

	private int id = -1;
	/**
	 * 是否已经选中过答案了
	 */
	private boolean isSelectedEver;
	public void showAnswer() {
		// TODO Auto-generated method stub
		super.showAnswer();

		RadioGroup radioGroup = new RadioGroup(context);
		
		/**
		 * 单选题
		 */
		for (int i = 0; i < currentAnswerModelObjectList.size(); i++) {
			HsdModelAnswer object = currentAnswerModelObjectList.get(i);
			final RadioButton radioButton = new RadioButton(context);
//			radioButton.setBackgroundDrawable(getActivity().getResources().getDrawable(R.layout.radio_button_selector));
			radioButton.setId(i);
			radioButton.setTag(object);
			radioButton.setText(object.getName());
			radioButton.setTextColor(Color.BLACK);
			if (!TextUtils.isEmpty(opFlag) && opFlag.equals("scan")) {
				radioButton.setEnabled(false);
			}
			radioGroup.addView(radioButton);
			
			
            if (currrentSelectedHsdDataResultList != null && currrentSelectedHsdDataResultList.size() > 0){
            	if (object.getAnsweruuid().equals(currrentSelectedHsdDataResultList.get(0).getAnsweruuid())) {
//            		System.out.println(object.getName());
            		radioButton.setChecked(true);
            		id = 0;
				}/*else{
					radioButton.setChecked(false);
				}*/
            	radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						try {
							// TODO Auto-generated method stub
							
							System.out.println("update------------");
							
							id = group.getCheckedRadioButtonId();
							RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
							
							String answertext = currentRadioButton.getText().toString();
							HsdModelAnswer hsdModelAnswer = (HsdModelAnswer) currentRadioButton.getTag();
//							System.out.println("updateQuestionResult----answertext---->"+hsdModelAnswer.getAnsweruuid()+answertext);
							DBController.getInstance().updateQuestionResult(researchuuid, surveyuuid,  hsdModelAnswer.getAnsweruuid(), hsdModelAnswer.getQuestionuuid(),answertext, context,dealAnswerResultHandler);
						} catch (Exception e) {
							// TODO: handle exception
							System.out.println("radioGroup.setOnCheckedChangeListener--excep----"+e.getMessage());
						}
						
					}
				});
            	
            }else {
            	radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    				@Override
    				public void onCheckedChanged(RadioGroup group, int checkedId) {
    					// TODO Auto-generated method stub
    					id = group.getCheckedRadioButtonId();
    					RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
    					String answertext = currentRadioButton.getText().toString();
    					HsdModelAnswer hsdModelAnswer = (HsdModelAnswer) currentRadioButton.getTag();
    					/**
    					 * 防止在一个界面不跳转的情况下,插入多次数据.
    					 */
    					if (isSelectedEver) {
    						System.out.println("update------------");
    						isSelectedEver = true;
    						DBController.getInstance().updateQuestionResult(researchuuid, surveyuuid,  hsdModelAnswer.getAnsweruuid(), hsdModelAnswer.getQuestionuuid(),answertext, context,dealAnswerResultHandler);
    					}else {
    						/**
    						 * 只有第一次触发才是插入操作，其它情况肯定是更新操作。
    						 */
    						isSelectedEver = true;
    						System.out.println("insert------------");
    						DBController.getInstance().insertQuestionResult(researchuuid,surveyuuid, hsdModelAnswer.getAnsweruuid(), hsdModelAnswer.getQuestionuuid(),answertext, context,dealAnswerResultHandler);
    					}
    					
    					System.out.println("insertQuestionResult---->"+hsdModelAnswer.getAnsweruuid());
    					
    				}
    			});
			}
        	
			
            
		}
		answerParentLayout.addView(radioGroup);
	}
	
	public boolean isHasResult(){
		if(id >= 0){
			return true;
		}else {
			return false;
		}
	}
	
}
